package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageBeanVo findVideoList(Integer page, Integer pageSize, Long userId) {
        int startIndex = (page - 1) * pageSize;
        //查询推荐视频
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .skip(startIndex).limit(pageSize);
        List<RecommendVideo> recommendVideoList = mongoTemplate.find(query, RecommendVideo.class);
        List<Video> videoList = new ArrayList<>();
        //如果推荐视频列表不为空
        if (CollectionUtil.isNotEmpty(recommendVideoList)) {
            //循环遍历推荐列表,查询视频详细信息
            for (RecommendVideo recommendVideo : recommendVideoList) {
                Video video = mongoTemplate.findById(recommendVideo.getVideoId(), Video.class);
                videoList.add(video);
            }
        }
        //查询总记录数
        long count = mongoTemplate.count(query, RecommendVideo.class);
        //封装返回对象
        return new PageBeanVo(page, pageSize, count, videoList);
    }

    @Autowired
    private IdService idService;

    /**
     * 上传视频
     *
     * @param video 视频对象
     */
    @Override
    public void saveVideo(Video video) {
        //设置上传时间
        video.setCreated(System.currentTimeMillis());
        //设置大数据视频id
        video.setVid(idService.getNextId(ConstantUtil.MOVEMENT_ID));
        mongoTemplate.save(video);
    }

    /**
     * 查询用户发布的视频列表
     * @param page 页码
     * @param pageSize 每页条数
     * @param uid 用户id
     * @return
     */
    @Override
    public PageBeanVo findUserVideoList(Integer page, Integer pageSize, Long uid) {

        int startIndex = (page - 1) * pageSize;
        Query query = new Query(Criteria.where("userId").is(uid))
                        .with(Sort.by(Sort.Order.desc("created")))
                        .skip(startIndex).limit(pageSize);
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        long count = mongoTemplate.count(query, Video.class);
        return new PageBeanVo(page,pageSize,count,videoList);
    }

    /**
     * 根据id查询视频信息
     * @param videoId 视频id
     * @return 视频对象
     */
    @Override
    public Video findById(String videoId) {
        return mongoTemplate.findById(new ObjectId(videoId),Video.class);
    }

}
